package cn.finedo.aiability;
import cn.finedo.aiability.service.AiAbilityHttpClient;

public class AiAbilityV1 {
	private String url;
	private String appKey;
	private String appId;
	private Integer timeout;
	private String param;

	public AiAbilityV1(String url, String param) {
		this.url = url;
		this.param = param;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public String callServer() throws Exception {
		if (this.timeout == null && isEmpty(this.appId)) {
			return api(this.url, this.param);
		} else if (this.timeout == null && !isEmpty(this.appId)) {
			return api(this.url, this.param, this.appId, this.appKey);
		} else if (this.timeout != null && isEmpty(this.appId)) {
			return api(this.url, this.param, this.timeout);
		} else if (this.timeout != null && !isEmpty(this.appId)) {
			return api(this.url, this.param, this.appId, this.appKey, this.timeout);
		}
		throw new RuntimeException("缺少必要参数");
	}

	public String api(String url, String param, String appId, String appKey, Integer timeout) throws Exception {
		return AiAbilityHttpClient.sendRequest(url, param, appId, appKey, timeout);
	}

	public String api(String url, String param, String appid, String appKey) throws Exception {
		return AiAbilityHttpClient.sendRequest(url, param, appid, appKey);
	}

	public String api(String url, String param, Integer timeout) throws Exception {
		return AiAbilityHttpClient.sendRequest(url, param, timeout);
	}

	public String api(String url, String param) throws Exception {
		return AiAbilityHttpClient.sendRequest(url, param);
	}

	boolean isEmpty(String param) {
		return param == null || param.length() == 0;
	}
}
